﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Newtonsoft.Json;

namespace Happy.Event.Offline
{
    /// <summary>
    /// 离线事件。
    /// </summary>
    public sealed class OfflineEvent
    {
        /// <summary>
        /// 事件类型。
        /// </summary>
        public string EventType { get; set; }

        /// <summary>
        /// 事件序列化为JSON后的字符串表示。
        /// </summary>
        public string EventBody { get; set; }

        internal object ToEvent()
        {
            return JsonConvert
                .DeserializeObject(
                    this.EventBody,
                    Type.GetType(this.EventType)
                );
        }

        internal static OfflineEvent Create(object @event)
        {
            var offlineEvent = new OfflineEvent();

            offlineEvent.EventType = @event.GetType().AssemblyQualifiedName;
            offlineEvent.EventBody = JsonConvert.SerializeObject(@event);

            return offlineEvent;
        }
    }
}
